package com.amkai.aes.webservice.binary.objecthandler.registration;

import java.io.Serializable;

import com.amkai.aes.aescommon.factory.RADBeanFactory;
import com.amkai.aes.aescommon.service.binary.job.registration.BgSpecificPropertiesJob;
import com.amkai.aes.aescommon.service.binary.job.registration.BgSpecificPropertiesJobResult;
import com.amkai.aes.generatedbeans.organization.BusinessGroup;
import com.amkai.aes.generatedbeans.organization.Department;
import com.amkai.aes.generatedbeans.registration.BgSpecificProperties;
import com.amkai.aes.generatedbeans.registration.Person;
import com.amkai.aes.webservice.binary.JobObjectHandler;
import com.amkai.common.extensions.hibernate.config.SiteManager;
import com.amkai.common.extensions.hibernate.config.manager.SessionManager;

public class BgSpecificPropertiesJobHandler implements JobObjectHandler<BgSpecificPropertiesJob, Serializable, BgSpecificPropertiesJobResult> {

	@Override
	public BgSpecificPropertiesJobResult process(BgSpecificPropertiesJob job, Serializable object) throws Exception {
		// TODO Auto-generated method stub
		BgSpecificPropertiesJobResult result = new BgSpecificPropertiesJobResult();
		if(job.getDepartmentId() != null && job.getPersonId() != null){
			SessionManager sm = SiteManager.Start();
			Department department = sm.get(Department.class, job.getDepartmentId());
			BusinessGroup businessGroup = sm.get(BusinessGroup.class, department.getParent().getId());
			BgSpecificProperties bgsp =  (BgSpecificProperties) sm.execute("FROM BgSpecificProperties bgsp WHERE bgsp.person.id = :personid AND bgsp.businessGroup = :bg ").setLong("personid", job.getPersonId()).setEntity("bg", businessGroup).uniqueResult();
			if(bgsp == null){
				Person person = sm.get(Person.class, job.getPersonId());
				bgsp = RADBeanFactory.getInstance().createBean(BgSpecificProperties.class);
				bgsp.setPerson(person);
				bgsp.setBusinessGroup(businessGroup);
				bgsp.setIncludeMPI(job.isIncludeMPISearch());
				sm.saveRADInTransaction(bgsp);
			}
			bgsp.getBusinessGroup().getName();
			if(bgsp.getEmployer() != null){
				bgsp.getEmployer().getName();
			}
			if(bgsp.getEthnicity() != null){
				bgsp.getEthnicity().getName();
			}
			if(bgsp.getLanguage() != null){
				bgsp.getLanguage().getName();
			}
			if(bgsp.getMaritalStatus() != null){
				bgsp.getMaritalStatus().getName();
			}
			if(bgsp.getOccupation() != null){
				bgsp.getOccupation().getName();
			}
			if(bgsp.getRace() != null){
				bgsp.getRace().getName();
			}
			if(bgsp.getReligion() != null){
				bgsp.getReligion().getName();
			}
			result.setBgSpecificProperties( bgsp );

		}
		return result;
		
	}

}
